package com.evil.account.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.account.enums.log.AccountLogTypeEnum;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.RedisUtil;
import com.evil.common.core.util.StreamUtil;
import com.evil.common.db.mapper.BatchMapper;
import com.evil.common.log.entity.Operator;
import com.evil.log.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Service
public class AccountLogServiceImpl extends LogService {

    private final LoginUtil loginUtil;

    public AccountLogServiceImpl(RedisUtil redisUtil, LoginUtil loginUtil) {
        super(redisUtil);
        this.loginUtil = loginUtil;
    }

    @SafeVarargs
    @Override
    public final <T, ID extends Serializable, M extends BaseMapper<T>> void saveAndCheck(M mapper, Function<T, ID> getId, T... bean) {
        this.saveAndCheck(mapper, getId, Arrays.asList(bean));
    }

    @Override
    public <T, ID extends Serializable, M extends BaseMapper<T>> void saveAndCheck(M mapper, Function<T, ID> getId, Collection<T> beans) {
        if (this.save(mapper, getId, beans) != beans.size()) {
            throw new BusinessException(RCodeEnum.UPDATE_ERROR);
        }
    }

    @SafeVarargs
    @Override
    public final <T, ID extends Serializable, M extends BaseMapper<T>> int save(M mapper, Function<T, ID> getId, T... bean) {
        return this.save(mapper, getId, Arrays.asList(bean));
    }

    @Override
    public <T, ID extends Serializable, M extends BaseMapper<T>> int save(M mapper, Function<T, ID> getId, Collection<T> beans) {
        if (!beans.isEmpty()) {
            UserBaseRespDTO loginUserBase = loginUtil.getLoginUserBase();
            // 当前登录人
            Operator operator = new Operator(loginUserBase.getUserId(), loginUserBase.getNickName());
            // 操作日志类型
            AccountLogTypeEnum logTypeEnum = AccountLogTypeEnum.findByClass(StreamUtil.findFirst(beans).getClass());
            return this.save(mapper, getId, beans, logTypeEnum, operator);
        }
        return 0;
    }

    @Override
    public <T, ID extends Serializable, M extends BatchMapper<T, ID>> void saveBatchAndCheck(M batchMapper, Function<T, ID> getId, Collection<T> beans) {
        if (!beans.isEmpty()) {
            UserBaseRespDTO loginUserBase = loginUtil.getLoginUserBase();
            // 当前登录人
            Operator operator = new Operator(loginUserBase.getUserId(), loginUserBase.getNickName());
            // 操作日志类型
            AccountLogTypeEnum logTypeEnum = AccountLogTypeEnum.findByClass(StreamUtil.findFirst(beans).getClass());
            if (this.saveBatch(batchMapper, getId, beans, logTypeEnum, operator) != beans.size()) {
                throw new BusinessException(RCodeEnum.UPDATE_ERROR);
            }
        }
    }
}
